package edu.mobbuzz.page.menu.rsschannel;

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import edu.mobbuzz.bean.RSSChannel;
import edu.mobbuzz.messaging.RequestStream;
import edu.mobbuzz.page.menu.Menu;
import edu.mobbuzz.util.string.MenuString;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

public class RSSSubscriptionForm extends Menu {
    private Form subscriptionForm;
    private Container subscriptionPanel;
    private Label menuTitle;
    private Label titleLbl, sourceLbl;
    private TextField titleTxt;
    private TextArea sourceTxtArea;
    private Command backCmd, subscribeCmd;
    private RSSChannel rssChannel;
    private int mode;
   
    
    public RSSSubscriptionForm(){
        subscriptionForm = new Form();
        subscriptionPanel = new Container();
        menuTitle = new Label(getMenuTitle());
        
        titleLbl = new Label();
        sourceLbl = new Label();

        titleTxt = new TextField();
        sourceTxtArea = new TextArea(5,20);
        
        backCmd = new Command("Back") {
            public void actionPerformed(ActionEvent evt) {
                backToRSSChannelPage();
            }
        };
        
        subscribeCmd = new Command("Subscribe") {
            public void actionPerformed(ActionEvent evt) {
                subscribeRSSChannel();
            }
        };
        
        
        
    }

    public RSSChannel getRssChannel() {
        return rssChannel;
    }

    public void setRssChannel(RSSChannel rssChannel) {
        this.rssChannel = rssChannel;
    }

    public int getMode() {
        return mode;
    }

    public void setMode(int mode) {
        this.mode = mode;
    }
    
    public void showPage() {
        
        if(getRssChannel()!=null){
            titleTxt.setText(getRssChannel().getTitle());
            sourceTxtArea.setText(getRssChannel().getSource());
        }
        
        menuTitle.setAlignment(Component.CENTER);
        menuTitle.getStyle().setMargin(5, 0, 5, 0);
        menuTitle.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        menuTitle.getStyle().setBgTransparency(0);
        
        titleLbl.setText("Title :");
        titleLbl.getStyle().setMargin(5, 0, 10, 2);
        titleLbl.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        titleLbl.getStyle().setBgTransparency(0);  
        
        titleTxt.getStyle().setMargin(2, 0, 10, 10);
        
        sourceLbl.setText("URL :");
        sourceLbl.getStyle().setMargin(5, 0, 10, 2);
        sourceLbl.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        sourceLbl.getStyle().setBgTransparency(0);  

        sourceTxtArea.setConstraint(TextArea.URL);
        sourceTxtArea.getStyle().setMargin(2, 0, 10, 10);
        
        
        
        subscriptionPanel.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        subscriptionPanel.addComponent(menuTitle);
        subscriptionPanel.addComponent(titleLbl);
        subscriptionPanel.addComponent(titleTxt);
        subscriptionPanel.addComponent(sourceLbl);
        subscriptionPanel.addComponent(sourceTxtArea);
        
        subscriptionForm.setLayout(new BorderLayout()); 
        subscriptionForm.addComponent(BorderLayout.CENTER, subscriptionPanel);
        subscriptionForm.addCommand(backCmd); 
        subscriptionForm.addCommand(subscribeCmd);
        subscriptionForm.setCommandListener(getActionlistener());
        subscriptionForm.show();
    }
    
    public void backToRSSChannelPage(){
        RSSChannelPage rssChannelPage = new RSSChannelPage();
        rssChannelPage.setExitCmd(getExitCmd());
        rssChannelPage.setActionlistener(getActionlistener());
        rssChannelPage.showPage();
    }

    public void subscribeRSSChannel() {
        String rssURL = "";

        
        
        if(titleTxt.getText()==null||titleTxt.getText().equals("")||
           sourceTxtArea.getText()==null||sourceTxtArea.getText().equals("")){
            Dialog.show("Error", "Title and URL must not be left blank", Dialog.TYPE_ERROR, null, "Ok", null);
        }else{

            if(sourceTxtArea.getText().startsWith("http://www.")){
                rssURL = sourceTxtArea.getText();
            }else if(sourceTxtArea.getText().startsWith("www.")){
                rssURL = "http://" + sourceTxtArea.getText();
            }else{
                rssURL = "http://www." + sourceTxtArea.getText();
            }

            String content = "";
            try{
                HttpConnection url = (HttpConnection) Connector.open(rssURL);
                int status = url.getResponseCode();
                if (status != HttpConnection.HTTP_OK)
                    content = "failed";
                else
                    content = "connected";
                if (url != null)
                    url.close();

                if(content.equals("connected")){                    
                    RSSChannel rssChannelObj = new RSSChannel();
                    rssChannelObj.setTitle(titleTxt.getText());
                    rssChannelObj.setSource(rssURL);
                    rssChannelObj.setLabel("");
                    
                    //getMessaging().getUsername()+"@"+getMessaging().getDomain()
                    StringBuffer request = RequestStream.sendRssRequest("reg", titleTxt.getText(), sourceTxtArea.getText());
                    getMessaging().sendMessage("news@mobbuzz.com",request.toString());

                    RSSChannelPage rssChannelPage = new RSSChannelPage();
                    rssChannelPage.setExitCmd(getExitCmd());
                    rssChannelPage.setMessaging(getMessaging());
                    rssChannelPage.showPage();
                }else{
                    Dialog.show("Error", "URL Error", Dialog.TYPE_ERROR, null, "Ok", null);
                }

            }catch(Exception e){
                Dialog.show("Error", "URL Error", Dialog.TYPE_ERROR, null, "Ok", null);
                e.printStackTrace();
            }                   
        }
        
    }
    
    public String getMenuTitle() {
        return MenuString.RSSCHANNEL;
    }

    
	 
}
 
